package com.base.common.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.common.DBAccess;
import com.base.model.EntityData;
import com.base.model.MessageInfo;

public class MessageDAO {
	
private DBAccess dbAccess = new DBAccess();

	
	public DBAccess getDbAccess() {
		return dbAccess;
	}


	public void setDbAccess(DBAccess dbAccess) {
		this.dbAccess = dbAccess;
	}
	
	public MessageInfo load(String id){
		MessageInfo info = new  MessageInfo();
		List<MessageInfo> resultList = new ArrayList<MessageInfo>();
		try {
			if(dbAccess.createConn()){
				String sql = "select * from tb_message_info where MESSAGE_ID = '" + id + "'";
				dbAccess.query(sql);
				if(dbAccess.next()){
					ResultSet rs = dbAccess.getRs();
					MessageInfo entity = new  MessageInfo();
					entity.setMessageId(rs.getString("MESSAGE_ID"));
					entity.setMessageSubject(rs.getString("MESSAGE_SUBJECT"));
					entity.setMessageContent(rs.getString("MESSAGE_CONTENT"));
					entity.setContactEmail(rs.getString("CONTACT_EMAIL"));
					entity.setContactName(rs.getString("CONTACT_NAME"));
					entity.setContactSex(rs.getString("CONTACT_SEX"));
					entity.setContactTel(rs.getString("CONTACT_TEL"));
					entity.setMessageViewFlag(rs.getString("MESSAGE_VIEW_FLAG"));
//					entity.setCreateUserId(rs.getString("CREATE_USER_ID"));
					entity.setCreateTime(rs.getDate("CREATE_TIME"));
//					entity.setUpdateUserId(rs.getString("UPDATE_USER_ID"));
//					entity.setUpdateTime(rs.getDate("UPDATE_TIME"));
//					entity.setDeleteFlag(rs.getString("DELETE_FLAG"));
					resultList.add(entity);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbAccess.closeRs();
			dbAccess.closeStm();
			dbAccess.closeConn();
		}
		if(resultList.size() > 0){
			info = resultList.get(0);
		}
		return info;
	}
	
	public boolean insert(MessageInfo info){
		boolean result = false;
		try {
			if(dbAccess.createConn()){
				String sql = "INSERT INTO tb_message_info "+
						"(MESSAGE_ID, MESSAGE_SUBJECT, MESSAGE_CONTENT, CONTACT_EMAIL, CONTACT_NAME, CONTACT_SEX," +
						" CONTACT_TEL, MESSAGE_VIEW_FLAG" +
						", CREATE_TIME) " +
						"VALUES ('" + info.getMessageId() + "', '" + info.getMessageSubject() + "', '" + info.getMessageContent() + "', '" + info.getContactEmail() + "'," +
								" '" + info.getContactName() + "', '" + info.getContactSex() + "', '" + info.getContactTel() + "', '0', '" + new Date() + "')";
				dbAccess.update(sql);
				result = true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbAccess.closeRs();
			dbAccess.closeStm();
			dbAccess.closeConn();
		}
		return result;
	}
	
	public List<? extends EntityData> list(){
		List<MessageInfo> results = new ArrayList<MessageInfo>();
		try {
			if(dbAccess.createConn()){
				String sql = "select * from tb_message_info order by MESSAGE_VIEW_FLAG ASC ,CREATE_TIME ASC";
				dbAccess.query(sql);
				while(dbAccess.next()){
					ResultSet rs = dbAccess.getRs();
					MessageInfo entity = new  MessageInfo();
					entity.setMessageId(rs.getString("MESSAGE_ID"));
					entity.setMessageSubject(rs.getString("MESSAGE_SUBJECT"));
					entity.setMessageContent(rs.getString("MESSAGE_CONTENT"));
					entity.setContactEmail(rs.getString("CONTACT_EMAIL"));
					entity.setContactName(rs.getString("CONTACT_NAME"));
					entity.setContactSex(rs.getString("CONTACT_SEX"));
					entity.setContactTel(rs.getString("CONTACT_TEL"));
					entity.setMessageViewFlag(rs.getString("MESSAGE_VIEW_FLAG"));
//					entity.setCreateUserId(rs.getString("CREATE_USER_ID"));
					entity.setCreateTime(rs.getDate("CREATE_TIME"));
//					entity.setUpdateUserId(rs.getString("UPDATE_USER_ID"));
////					entity.setUpdateTime(rs.getDate("UPDATE_TIME"));
//					entity.setDeleteFlag(rs.getString("DELETE_FLAG"));
					results.add(entity);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbAccess.closeRs();
			dbAccess.closeStm();
			dbAccess.closeConn();
		}
		return results;
	}
	
	public boolean delete(String id){
		boolean result = false;
		try {
			if(dbAccess.createConn()){
				String sql = "delete from tb_message_info where MESSAGE_ID = '"+id +"'";
				dbAccess.update(sql);
				result = true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbAccess.closeRs();
			dbAccess.closeStm();
			dbAccess.closeConn();
		}
		return result;
	}
	public boolean update(MessageInfo info){
		boolean result = false;
		try {
			if(dbAccess.createConn()){
				String sql = "update tb_message_info "+
						"set MESSAGE_VIEW_FLAG = '" + info.getMessageViewFlag() + "'" +
								" where MESSAGE_ID ='" + info.getMessageId() + "'";
				dbAccess.update(sql);
				result = true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbAccess.closeRs();
			dbAccess.closeStm();
			dbAccess.closeConn();
		}
		return result;
	}
}
